﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Configuration.Provider;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web.Configuration;

namespace JeanFish.Blog.Storage
{
    public class BlogProviderHelper
    {
        private static BlogProviderCollection Providers<T>(string blogProviderSettingSectionName, out BlogProviderSettingSection section) where T : ProviderBase
        {
            section =
                ConfigurationManager.GetSection(blogProviderSettingSectionName) as BlogProviderSettingSection;
            var providerCollection = new BlogProviderCollection();
            // this wl instatiate PersonProvider with the class "personimpl" which inherit it
            ProvidersHelper.InstantiateProviders(section.Providers, providerCollection, typeof(T));
            providerCollection.SetReadOnly();
            return providerCollection;
        }
        private static IBlogDataProvider defaultDataProvider = null;
        public static IBlogDataProvider GetDefaultDataProvider(string blogProviderSettingSectionName)
        {
                if(defaultDataProvider==null)
                {
                    BlogProviderSettingSection section=null;
                    defaultDataProvider=Providers<BlogDataProvider>(blogProviderSettingSectionName,out section)[section.DefaultProvider] as IBlogDataProvider;
                }
                return defaultDataProvider;
        }
        private static IBlogMediaProvider defaultMediaProvider = null;
        public static IBlogMediaProvider GetDefaultMediaProvider(string blogProviderSettingSectionName)
        {
            if (defaultMediaProvider == null)
            {
                BlogProviderSettingSection section = null;
                defaultMediaProvider = Providers<BlogMediaProvider>(blogProviderSettingSectionName, out section)[section.DefaultProvider] as IBlogMediaProvider;
            }
            return defaultMediaProvider;
        }
    }
}
